package com.fly.flowdemo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with Android Studio.
 * Package_Name：com.fly.flowdemo.view
 * Project_Name：FlowDemo
 * User：郭鹏飞
 * Date：2015/12/11
 * Email：love518420@foxmail.com
 * Description：多选按钮流式布局
 */
public class MulButtonFlowLayout extends ButtonFlowLayout {

    private List<View> chooseViewList;
    private List<Integer> choosePositionList;

    private OnItemChoosedListener listener;

    public MulButtonFlowLayout(Context context) {
        this(context, null);
    }

    public MulButtonFlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MulButtonFlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        chooseViewList = new ArrayList<>();
        choosePositionList = new ArrayList<>();
    }

    @Override
    protected void dealWithClickEvent(Button button, int position) {
        // 判断当前的view是否被选中了 如果选中则清除选中状态
        if (chooseViewList.contains(button)) { // 已经被选中过
            button.setBackgroundResource(normlaResId); // 切换背景
            chooseViewList.remove(button); // 移除该位置
            choosePositionList.remove(Integer.valueOf(position));
        } else {
            // 设置当前选中
            button.setBackgroundResource(selectedResId);
            // 添加该位置
            chooseViewList.add(button);
            choosePositionList.add(position);
        }
        // 添加回调
        if (listener != null) {
            listener.onItemChoosed(chooseViewList, choosePositionList);
        }
    }

    public void setOnItmChoosedListener(OnItemChoosedListener listener) {
        this.listener = listener;
    }

    @Override
    public void clearChooseStatus() {
        super.clearChooseStatus();
        chooseViewList.clear();
        choosePositionList.clear();
    }

    public interface OnItemChoosedListener {
        /**
         * 选中回调
         *
         * @param views    被选中view集合
         * @param positions 被选中位置集合
         */
        void onItemChoosed(List<View> views, List<Integer> positions);
    }

}
